<?php

/**
*	This class is a data model object for representation of address information.
*
*/
class Address extends ORDataObject {

	var $address_id			= '';
	var $street		= '';
	var $province_id	= '';
	var $district_id		= '';
	var $ward_name		= '';
	
	var $_key='address_id';
	var $_table='address';
	var $_internalName='Address';

	var $_foreignKeyList=array(
		'province_id'=>'province',
		'district_id'=>'district'
	);
	public function Address($db = null) {
		parent::ORDataObject($db);
	}
	
	public function setup($id = 0) {
		if ($id !== 0) {
			$this->setId($id);
			$this->populate();
		}

	}

	/**
	* Pull data for this record from the database
	*/
	public function populate() {
		parent::populate('address_id');		
	}
	
	/**
	* Delete this record
	*/
	public function drop()
	{
		$this->_execute("delete from {$this->_prefix}$this->_table where address_id = ". (int)$this->id);
	}
	
	public function getId(){
		return $this->address_id;
	}
	
	public function setId($id){
		$this->address_id=$id;
	}
	
    public function getProvinceId() {
    	return $this->province_id;
    }
    
    public function getProvinceName(){
    	$field =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
    	$sql = "select t.$field as name from province t  
    	        where province_id=".(int)$this->province_id;
    	$res = $this->_execute($sql);
    	return $res->fields['name'];
    }
    
    public function getDistrictId() {
    	return $this->district_id;
    }
    
	public function getDistrictName(){
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
    	$sql = "SELECT t.$field as name from district t  
    	        WHERE district_id=".(int)$this->district_id;
    	$res = $this->_execute($sql);
    	return $res->fields['name'];
    }
    
    public function getWardName() {
    	return $this->ward_name;
    }
    
    public function getStreet(){
    	return $this->street;
    }
    
    public function getAddress() {
    	return L10nHelper::getAddress($this->getProvinceName(), $this->getDistrictName(), $this->getWardName(), $this->getStreet());
    }
    
    public function delete($addressId) {
    	$sql	=	"DELETE FROM $this->_table WHERE $this->_key = '$addressId'";
    	$this->_db->execute($sql);
    }
} 
?>
